#ifndef skyBuffer_h__
#define skyBuffer_h__

#include "./skyObject.h"

namespace Sky
{
	class Buffer;
	typedef RefPtr<Buffer> BufferPtr;
	class Buffer : public Object
	{
	private:
		class bufferNode
		{
		protected:
			friend class Buffer;
			u8* _buff;
			u8* _head;
			int   _len;
			int   _size;
		public:
			bufferNode(BufferPtr& buff):_buff(buff->_buff),_size(buff->_size),_head(buff->_buff),_len(buff->_len){}
		};
	public:
		class staticBuffer : public bufferNode
		{

		public:
			staticBuffer(BufferPtr& buff):bufferNode(buff){}
		};

		class dynamicBuffer : public bufferNode
		{
		public:
			dynamicBuffer(BufferPtr& buff):bufferNode(buff){}
		};
	public:
		Buffer(u8* buff,int size):_buff(buff),_size(size),_head(_buff),_len(0){}
		u8* getBuffer(){return _buff;}
		TInt getSize(){return _size;}
	private:
		u8* _buff;
		u8* _head;
		int   _len;
		int   _size;
	};

	//////////////////////////////////////////////////////////////////////////
	class BufferBuilder : public Object
	{
	public:
		BufferBuilder();
		~BufferBuilder();
		BufferBuilder* append(BufferPtr& buf);
		//BufferBuilder* append(Buffer::staticBuffer buf);
		//BufferBuilder* append(Buffer::dynamicBuffer buf);
		BufferBuilder* append(const char* buf,int size);
		BufferPtr toBuffer();
		void clear();
	private:
		class BufferNode : public Memory
		{
		public:
			BufferPtr buff;
			BufferNode* next;
		};
		BufferNode* _head;
		BufferNode* _read;
		int _totalSize;
	};
	typedef RefPtr<BufferBuilder> BufferBuilder$;
}

#endif // skyBuffer_h__